//
//  GWRecommentViewController.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/2.
//  Copyright © 2016年 高炜. All rights reserved.
//


#import "GWRecommentViewController.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import <MJExtension.h>
#import "GWRecomendCategary.h"//
#import "GWRecomendCategaryCell.h"
#import "GWRecommenUser.h"
#import "GWRecommendUserCell.h"
#import <MJRefresh.h>

#define CurrentCategary self.categarys[self.recomondCategaryView.indexPathForSelectedRow.row]
@interface GWRecommentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *recomondCategaryView;
@property (weak, nonatomic) IBOutlet UITableView *recommendUserView;

@property (nonatomic,copy)NSArray *categarys;
@end

static NSString *categaryIdentifier =@"categary";
static NSString *userIdentifier =@"user";
@implementation GWRecommentViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title =@"推荐关注";
    self.view.backgroundColor =GWGlobalBGColor;
    
    [self setUpTableView];
    
    [self setUpRefershView];
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    NSDictionary *parames =@{@"a":@"category",@"c":@"subscribe"};
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.categarys =[GWRecomendCategary mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        [self.recomondCategaryView reloadData];
        [self.recomondCategaryView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
    }];
   
}
-(void)setUpRefershView
{
    self.recommendUserView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreUser)];
    self.recommendUserView.mj_footer.hidden =YES;
    
    self.recommendUserView.mj_header =[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewUser)];
}
-(void)checkFooterStatus
{
     GWRecomendCategary *categary =CurrentCategary;
    self.recommendUserView.mj_footer.hidden =[CurrentCategary users].count==0;
    if (categary.users.count==[categary.total integerValue]) {
        [self.recommendUserView.mj_footer endRefreshingWithNoMoreData];
    }
    else
    {
        [self.recommendUserView.mj_footer endRefreshing];
    }
}
-(void)loadNewUser
{
    GWRecomendCategary *categary =CurrentCategary;
    categary.currentPage =1;
    NSDictionary *parames =@{@"a":@"list",@"c":@"subscribe",@"category_id":categary.id,@"page":@(categary.currentPage)};
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [categary.users removeAllObjects];
        
        [categary.users addObjectsFromArray:[GWRecommenUser mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]];
        
        [self.recommendUserView reloadData];
        [self.recommendUserView.mj_header endRefreshing];
        [self checkFooterStatus];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
        [self.recommendUserView.mj_header endRefreshing];
    }];
}
-(void)loadMoreUser
{
    
    GWRecomendCategary *categary =CurrentCategary;
    NSString *categoryID =categary.id;
    NSDictionary *parames =@{@"a":@"list",@"c":@"subscribe",@"category_id":categoryID,@"page":@(++categary.currentPage)};
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        GWRecomendCategary *categary=CurrentCategary;
        [categary.users addObjectsFromArray:[GWRecommenUser mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]];
        
        [self.recommendUserView reloadData];
        
        [self checkFooterStatus];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.recommendUserView.mj_footer endRefreshing];
    }];
}
-(void)setUpTableView
{
    self.recomondCategaryView.backgroundColor =myColor(244, 244, 244);
    [self.recomondCategaryView registerNib:[UINib nibWithNibName:@"GWRecomendCategaryCell" bundle:nil] forCellReuseIdentifier:categaryIdentifier];
    
    [self.recommendUserView registerNib:[UINib nibWithNibName:@"GWRecommendUserCell" bundle:nil] forCellReuseIdentifier:userIdentifier];
    self.recommendUserView.rowHeight =70;
    
    self.automaticallyAdjustsScrollViewInsets =NO;
    self.recommendUserView.contentInset =UIEdgeInsetsMake(64, 0, 0, 0);
    self.recomondCategaryView.contentInset =UIEdgeInsetsMake(64, 0, 0, 0);
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.recomondCategaryView) {
        return self.categarys.count;
    }
    else
    {
        [self checkFooterStatus];
        return [CurrentCategary users].count;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (tableView==self.recomondCategaryView) {
        GWRecomendCategary *categary =self.categarys[indexPath.row];
        GWRecomendCategaryCell *cell =[tableView dequeueReusableCellWithIdentifier:categaryIdentifier];
        cell.categary =categary;
        return cell;
    }
    else
    {
        GWRecommenUser *user =[CurrentCategary users][indexPath.row];
        GWRecommendUserCell *cell =[tableView dequeueReusableCellWithIdentifier:userIdentifier];
        cell.user =user;
        return cell;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([CurrentCategary users].count) {
        
        [self.recommendUserView reloadData];
    }
    else
    {
        [self.recommendUserView reloadData];
        
        [self.recommendUserView.mj_header beginRefreshing];
        [self loadNewUser];
    }
    
    
}

@end
